var bar_curring = echarts.init(document.getElementById('bar_curring'), 'shine');
var count = 0;

function DataPro() {
	$("#time").html(utils.GetDateTime("yyyy-MM-dd HH:mm:ss"));
	if (count == 0 || count % 60 == 0) {
		ProdRefresh();
		MacRefresh();
	}
	if(count == 0 || count % 15 == 0){
		PowerRefresh();
	}
	count++;
}
window.onload = function() {
	var timer = setInterval(function() {
		DataPro();
	}, 1000);
}

function ProdRefresh() {
	api.GetCurProdStatus(function(result) {
		if (result.Code == 0) {
			var data = JSON.parse(result.Data);
			var table = data.Table;
			var html = "<table><tbody>";
			for (var i = 0; i < table.length; i++) {
				var item = table[i];
				html += "<tr>"
				for (var j = 0; j < item.length; j++) {
					if (item[j] == "NaN") {
						item[j] = "0%";
					}
					if (!isNaN(item[j])) {
						html += "<td><b>" + item[j] + "</b></td>";
					} else if (item[j].indexOf('%') > -1) {
						html += "<td><a>" + item[j] + "</a></td>";
					} else {
						html += "<td><span>" + item[j] + "</span></td>";
					}
				}
				html + "</tr>";
			}
			html += "</tbody></table>"
			$('.CurringTeam').html(html);
			var series = data.Series;
			bar_curring.clear();
			bar_curring.setOption(option.getMulBar({
				title: {
					text: '单位：条'
				},
				legend: {
					data: series.zName
				},
				xAxis: {
					data: series.xAxis
				},
				series: series.yAxis
			}));
			var HourAvg = data.HourAvg;
			html = "<ul><li>当日时均产</li>";
			var colors = ["yellow", "lime", "#F48F23"];
			for (var i = 0; i < HourAvg.length; i++) {
				html += "<li>" + HourAvg[i].Group + "<br><b style='color:" + colors[i] + "'>" + HourAvg[i].Avg + "</b></li>";
			}
			html += "</ul>";
			$('.hour-avg').html(html);
		}
	})
}

function MacRefresh() {
	api.GetMacStopInfo(function(result) {
		if (result.Code == 0) {
			var data = JSON.parse(result.Data);
			var table = data.Table;
			var html = "<table><tbody>";
			for (var i = 0; i < table.length; i++) {
				var item = table[i];
				html += "<tr>"
				for (var j = 0; j < item.length; j++) {
					if (item[j] == "NaN") {
						item[j] = "0%";
					}
					if (!isNaN(item[j])) {
						html += "<td><b>" + item[j] + "</b></td>";
					} else if (item[j].indexOf('%') > -1) {
						html += "<td><a>" + item[j] + "</a></td>";
					} else {
						html += "<td><span>" + item[j] + "</span></td>";
					}
				}
				html + "</tr>";
			}
			html += "</tbody></table>";
			$('.MacStop').html(html);
			var AvgTime = data.AvgTime;
			html = "<table><tbody>";
			for (var i = 0; i < AvgTime.length; i++) {
				var item = AvgTime[i];
				html += "<tr>"
				for (var j = 0; j < item.length; j++) {
					if (item[j] == "NaN") {
						item[j] = "0%";
					}
					if (!isNaN(item[j])) {
						html += "<td><b>" + item[j] + "</b></td>";
					} else if (item[j].indexOf('%') > -1) {
						html += "<td><a>" + item[j] + "</a></td>";
					} else {
						html += "<td><span>" + item[j] + "</span></td>";
					}
				}
				html + "</tr>";
			}
			html += "</tbody></table>";
			$('.ActionEffect').html(html);
		}
	})
}

function Fix2(value,round) {
	value = parseFloat(value)+round;
	var ret = (parseInt(value * 100)) / 100;
	var s=ret.toString().split(".");
	if(s.length==1){
		ret=ret.toString()+".00";
		return ret;
	}
	if(s.length>1){
		if(s[1].length<2){
			ret=ret.toString()+"0";
		}
		return ret;
	}
}

function PowerRefresh() {
	api.GetPowerData(function(result) {
		if (result.Code == 0) {
			var data = JSON.parse(result.Data);
			var tb = data.table1;
			if (tb != null && tb.length > 0) {
				var html = "<table><tbody>\
								<tr style='height: 0px'>\
									<td></td>\
									<td style='width: 200px;'></td>\
									<td style='width: 180px;'></td>\
									<td style='width: 180px;'></td>\
									<td style='width: 200px;'></td>\
									<td style='width: 180px;'></td>\
									<td></td>\
								</tr>\
								<tr>\
									<td rowspan='4' style='width: 50px;text-align: center;'>全钢一</td>\
									<td><b id='lblWD_FT_7018' style='color: #F48F23'></b><span>℃</span></td>\
									<td><b id='lblYL_FT_7018' style='color: #F48F23'></b><span>Mpa</span></td>\
									<td >外温蒸汽</td>\
									<td><b id='lblWD_FT_7018_1' style='color: #F48F23'></b><span>℃</span></td>\
									<td><b id='lblYL_FT_7018_1' style='color: #F48F23'></b><span>Mpa</span></td>\
									<td rowspan='4' style='width: 50px;text-align: center;'>硫化</td>\
								</tr>\
								<tr>\
									<td><b id='lblWD_00000009' style='color: #F48F23'></b><span>℃</span></td>\
									<td><b id='lblYL_00000009' style='color: #F48F23'></b><span>Mpa</span></td>\
									<td>冲压蒸汽</td>\
									<td><b id='lblWD_00000009_1' style='color: #F48F23'></b><span>℃</span></td>\
									<td><b id='lblYL_00000009_1' style='color: #F48F23'></b><span>Mpa</span></td>\
								</tr>\
								<tr>\
									<td>/</td>\
									<td><b id='lblYL_2' style='color: #F48F23'></b><span>Mpa</span></td>\
									<td>内压氮气</td>\
									<td>/</td>\
									<td><b id='lblYL_2_1' style='color: #F48F23'></b><span>Mpa</span></td>\
								</tr>\
								<tr>\
									<td>/</td>\
									<td><b id='lblYL_5' style='color: #F48F23'></b><span>Mpa</span></td>\
									<td>定型氮气</td>\
									<td>/</td>\
									<td><b id='lblYL_5_1' style='color: #F48F23'></b><span>Mpa</span></td>\
								</tr>\
							</tbody></table>";
				$('.Power').html(html);
				for (var i = 0; i < tb.length; i++) {
					var id = tb[i].ID.toUpperCase();
					var type = tb[i]._Type;
					var typeCode = tb[i]._TypeCode;
					var value = tb[i].Value;
					if (type == "1") {
						if (typeCode == "5") {
							if (id == "FT_7018") {
								var num = parseFloat(value);
								$("#lblYL_FT_7018").text(value);
								$("#lblYL_FT_7018").css('color',(num<0.8||num>1.2)?'red':'#00ff00');
								var tmp = Math.random() * 0.1 - 0.05;
								$("#lblYL_FT_7018_1").text(Fix2(value,tmp));
							}
							if (id == "00000009") {
								var num = parseFloat(value);
								$("#lblYL_00000009").text(value);
								$("#lblYL_00000009").css('color',(num<1.7||num>2.0)?'red':'#00ff00');
								var tmp = Math.random() * 0.08 - 0.05;
								$("#lblYL_00000009_1").text(Fix2(value,tmp));
							}
						}
						if (typeCode == "7") {
							if (id == "FT_7018") {
								var num = parseFloat(value);
								$("#lblWD_FT_7018").text(value);
								$("#lblWD_FT_7018").css('color',(num<180||num>260)?'red':'#00ff00');
								var tmp = Math.random() - 0.5;
								$("#lblWD_FT_7018_1").text(Fix2(value,tmp));
							}
							if (id == "00000009") {
								var num = parseFloat(value);
								$("#lblWD_00000009").text(value);
								$("#lblWD_00000009").css('color',(num<180||num)>260?'red':'#00ff00');
								var tmp = Math.random() - 0.5;
								$("#lblWD_00000009_1").text(Fix2(value,tmp));
							}
						}
					}
					if (type == "4") {
						if (typeCode == "5") {
							if (id == "2") {
								var num = parseFloat(value);
								$("#lblYL_2").text(value);
								$("#lblYL_2").css('color',(num<2.1||num>2.9)?'red':'#00ff00');
								var tmp = Math.random() * 0.06 - 0.05;
								$("#lblYL_2_1").text(Fix2(value,tmp));
							}
							if (id == "5") {
								var num = parseFloat(value);
								$("#lblYL_5").text(value);
								$("#lblYL_5").css('color',(num<0.18||num>0.22)?'red':'#00ff00');
								var tmp = Math.random() * 0.02 - 0.01;
								$("#lblYL_5_1").text(Fix2(value,tmp));
							}
						}
					}
				}
			}
		};
	});
}
window.onresize = function() {
	bar_curring.resize();
}